package org.springblade.modules.admin.task.feishu;

import cn.hutool.core.util.StrUtil;
import com.google.gson.JsonObject;
import com.lark.oapi.Client;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.bitable.v1.model.*;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.springblade.common.config.FeiShuConfiguration;
import org.springblade.common.enums.FeiShuDocumentEnum;
import org.springblade.common.enums.FeishuClientEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.UserSignDetailDTO;
import org.springblade.modules.admin.dto.feishu.FeiShuBatchDTO;
import org.springblade.modules.admin.dto.feishu.FeiShuFieldsDTO;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.SignStatusEnum;
import org.springblade.modules.admin.service.IUserSignDetailService;
import org.springblade.modules.admin.service.UserSignDetailService;
import org.springblade.modules.admin.service.feishu.IFeiShuDocumentService;
import org.springblade.modules.admin.vo.feishu.LarkElevatorDailyAttendanceStaticVO;
import org.springblade.modules.admin.vo.feishu.LarkUserSignDetailVO;
import org.springblade.modules.admin.vo.plan.PlanListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 飞书考勤数据表Job
 * @author Sharry
 * @since 2024-05-22
 */
@Component
@Slf4j
public class LarkElevatorDailyAttendanceJob {

	@Autowired
	private IFeiShuDocumentService feiShuDocumentService;

	@Autowired
	private UserSignDetailService userSignDetailService;

	@Autowired
	private FeiShuConfiguration feiShuConfiguration;

	@XxlJob("galaxyElevatorDailyAttendanceJobCopy")
	public void galaxyElevatorDailyAttendanceJobCopy(){
		XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]开始");
		try{
			// 1. 数据准备：appToken，tableId，要对比新增的数据
			String appToken = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_COPY.getAppToken();
			String tableId = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_COPY.getTableId();
			feiShuDocumentService.commandDistinctByKeyAndUpdateTime(appToken, tableId, "考勤记录ID（主键）","签到时间");
			// 查询当天对比新增的数据
			// 时间查询条件：今天 0:00 - 23:59
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			Date startTime = calendar.getTime();
			calendar.set(Calendar.HOUR_OF_DAY, 23);
			calendar.set(Calendar.MINUTE, 59);
			calendar.set(Calendar.SECOND, 59);
			Date endTime = calendar.getTime();
			String tenantId = "592800";
			// 根据时间及userId查询所需新增数据
			List<LarkUserSignDetailVO> targetList = userSignDetailService.getLarkSignDetailByCondition(
					tenantId, startTime, endTime, null
			);
			log.info("查询到新增数据：{}", targetList.size());
			if(Func.isEmpty(targetList)){
				return;
			}

			// 2. 数据处理、数据封装

			FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
			List<FeiShuFieldsDTO> records = new ArrayList<>();
			HashMap<Integer, String> weekMap = new HashMap<Integer,String>();
			weekMap.put(1, "一");
			weekMap.put(2, "二");
			weekMap.put(3, "三");
			weekMap.put(4, "四");
			weekMap.put(5, "五");
			weekMap.put(6, "六");
			weekMap.put(7, "日");
			for (LarkUserSignDetailVO t : targetList) {

				FeiShuFieldsDTO element = new FeiShuFieldsDTO();
				Map<String, Object> elementMap = new HashMap<>(30);
				if(!Func.isNull(t.getPlanType())){
					String firstSignTypeName = MaintainPlanContentEnum.getName(t.getPlanType());
					elementMap.put("当日触发首签的业务工单类型", firstSignTypeName);
				}
				elementMap.put("考勤记录ID（主键）", String.valueOf(t.getId()));
				elementMap.put("梯无忧_userID", String.valueOf(t.getTargetUserId()));
				elementMap.put("考勤日期", startTime.getTime());
				// 数字1到7分别代表星期一到星期日
				if(!Func.isNull(t.getWeek())){
					elementMap.put("星期", "星期"+weekMap.get(t.getWeek()));
				}
				elementMap.put("日期类型", t.getHoliday() == 0 ? "工作日" : "休息日");
				elementMap.put("姓名", t.getUserName());
				// 考勤类型，当前固定为维保
				elementMap.put("考勤类型", "维保");
				if(!Func.isNull(t.getSignTime())){
					DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					// 将String 转为 Date
					Date signTime = dateFormat.parse(t.getSignTime());
					elementMap.put("签到时间", signTime.getTime());
				}

				elementMap.put("签到地点", t.getSignAddress());
				if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.NORMAL.getId()) {
					if (t.getSignType() != null) {
						if (t.getSignType() == 2) {
							elementMap.put("签到状态", "异常");
							elementMap.put("签到说明", "范围外打卡");
						} else {
							elementMap.put("签到状态", "正常");
						}
					} else {
						elementMap.put("签到状态", "正常");
					}
				} else {
					elementMap.put("签到状态", "异常");
					String i = "";
					if (t.getSignType() != null) {
						i = t.getSignType() == 2 ? "-范围外打卡" : "";
					}
					if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.BE_LATE.getId()) {
						elementMap.put("签到说明", "迟到" + i);
					} else if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.ABSENTEEISM.getId()) {
						elementMap.put("签到说明", "缺勤" + i);
					}
				}
				if (StrUtil.isNotEmpty(t.getNewSignDesc())) {
					elementMap.put("签到补卡申请", t.getNewSignDesc());
				}

				if(!Func.isNull(t.getSignOutTime())){
					DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					// 将String 转为 Date
					Date signOutTime = dateFormat.parse(t.getSignOutTime());
					elementMap.put("签退时间", signOutTime.getTime());
				}
				elementMap.put("签退地点", t.getSignOutAddress());

				if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.NORMAL.getId()) {
					if (t.getSignOutType() != null) {
						if (t.getSignOutType() == 2) {
							elementMap.put("签退状态", "异常");
							elementMap.put("签退说明", "范围外打卡");
						} else {
							elementMap.put("签退状态", "正常");
							elementMap.put("签退说明", "正常");
						}
					} else {
						elementMap.put("签退状态", "正常");
						elementMap.put("签退说明", "正常");
					}
				} else {
					elementMap.put("签退状态", "异常");
					String i = "";
					if (t.getSignOutType() != null) {
						i = t.getSignOutType() == 2 ? "-范围外打卡" : "";
					}
					if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.BE_LATE.getId()) {
						elementMap.put("签退说明", "早退" + i);
					} else if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.ABSENTEEISM.getId()) {
						elementMap.put("签退说明", "缺勤" + i);
					}
				}
				if (StrUtil.isNotEmpty(t.getNewSignOutDesc())) {
					elementMap.put("签退补卡申请", t.getNewSignOutDesc());
				}
				if(!Func.isNull(t.getUserRoleName())){
					elementMap.put("业务角色", t.getUserRoleName().split(","));
				}
				elementMap.put("当日计划保养工单数", t.getRepairPlanCount());
				if(!Func.isNull(t.getPlanType())){
					String firstSignTypeName = MaintainPlanContentEnum.getName(t.getPlanType());
					elementMap.put("当日触发首签的业务工单类型", firstSignTypeName);
				}
				if(!Func.isNull(t.getTriggerTime())){
					elementMap.put("触发首签工单关联日期",t.getTriggerTime().getTime());
				}
				// 2024-07-15 新增首签工单定位异常情况
				if(!Func.isNull(t.getSignStatus())){
					String description = new Integer(1).equals(t.getSignStatus()) ? "正常"
						: new Integer(2).equals(t.getSignStatus()) ? "定位异常"
						: new Integer(3).equals(t.getSignStatus()) ? "离线签到"
						: "";
					elementMap.put("首签定位异常情况", description);
				}
				element.setFields(elementMap);
				records.add(element);
			}
			batchDTO.setRecords(records);
			// 3. 调用飞书API更新数据表、发送消息通知
			String finalResult = feiShuDocumentService.commandCompareAddBatch(appToken,tableId,batchDTO,"考勤记录ID（主键）");
			String message = "定时器或表格按钮触发更新"+FeiShuDocumentEnum.getDescription(appToken,tableId)+"结果：" + finalResult;
			feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]失败"+e);
		}
	}

	@XxlJob("galaxyElevatorDailyAttendanceJob")
	public void galaxyElevatorDailyAttendanceJob(){
		XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]开始");
		try{
			// 1. 数据准备：appToken，tableId，要对比新增的数据
			String appToken = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE.getAppToken();
			String tableId = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE.getTableId();
			feiShuDocumentService.commandDistinctByKeyAndUpdateTime(appToken, tableId, "考勤记录ID（主键）","签到时间");
			// 查询当天对比新增的数据
			// 时间查询条件：今天 0:00 - 23:59
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date startTime = calendar.getTime();
			calendar.set(Calendar.HOUR_OF_DAY, 23);
			calendar.set(Calendar.MINUTE, 59);
			calendar.set(Calendar.SECOND, 59);
			calendar.set(Calendar.MILLISECOND, 0);
			Date endTime = calendar.getTime();
			String tenantId = "592800";
			// 根据时间及userId查询所需新增数据
			List<LarkUserSignDetailVO> targetList = userSignDetailService.getLarkSignDetailByCondition(
				tenantId, startTime, endTime, null
			);
			log.info("查询到{}条考勤数据",targetList.size());
			if(Func.isEmpty(targetList)){
				return;
			}

			// 2. 数据处理、数据封装

			FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
			List<FeiShuFieldsDTO> records = new ArrayList<>();
			HashMap<Integer, String> weekMap = new HashMap<Integer,String>();
			weekMap.put(1, "一");
			weekMap.put(2, "二");
			weekMap.put(3, "三");
			weekMap.put(4, "四");
			weekMap.put(5, "五");
			weekMap.put(6, "六");
			weekMap.put(7, "日");
			for (LarkUserSignDetailVO t : targetList) {

				FeiShuFieldsDTO element = new FeiShuFieldsDTO();
				Map<String, Object> elementMap = new HashMap<>(30);
				if(!Func.isNull(t.getPlanType())){
					String firstSignTypeName = MaintainPlanContentEnum.getName(t.getPlanType());
					elementMap.put("当日触发首签的业务工单类型", firstSignTypeName);
				}
				elementMap.put("考勤记录ID（主键）", String.valueOf(t.getId()));
				elementMap.put("梯无忧_userID", String.valueOf(t.getTargetUserId()));
				elementMap.put("考勤日期", startTime.getTime());
				if(!Func.isNull(t.getGroupName())){
					elementMap.put("组名称", t.getGroupName().split(","));
				}
				// 数字1到7分别代表星期一到星期日
				if(!Func.isNull(t.getWeek())){
					elementMap.put("星期", "星期"+weekMap.get(t.getWeek()));
				}
				elementMap.put("日期类型", t.getHoliday() == 0 ? "工作日" : "休息日");
				elementMap.put("姓名", t.getUserName());
				// 考勤类型，当前固定为维保
				elementMap.put("考勤类型", "维保");
				if(!Func.isNull(t.getSignTime())){
					DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					// 将String 转为 Date
					Date signTime = dateFormat.parse(t.getSignTime());
					elementMap.put("签到时间", signTime.getTime());
				}

				elementMap.put("签到地点", t.getSignAddress());
				if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.NORMAL.getId()) {
					if (t.getSignType() != null) {
						if (t.getSignType() == 2) {
							elementMap.put("签到状态", "异常");
							elementMap.put("签到说明", "范围外打卡");
						} else {
							elementMap.put("签到状态", "正常");
						}
					} else {
						elementMap.put("签到状态", "正常");
					}
				} else {
					elementMap.put("签到状态", "异常");
					String i = "";
					if (t.getSignType() != null) {
						i = t.getSignType() == 2 ? "-范围外打卡" : "";
					}
					if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.BE_LATE.getId()) {
						elementMap.put("签到说明", "迟到" + i);
					} else if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.ABSENTEEISM.getId()) {
						elementMap.put("签到说明", "缺勤" + i);
					}
				}
				if (StrUtil.isNotEmpty(t.getNewSignDesc())) {
					elementMap.put("签到补卡申请", t.getNewSignDesc());
				}

				if(!Func.isNull(t.getSignOutTime())){
					DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					// 将String 转为 Date
					Date signOutTime = dateFormat.parse(t.getSignOutTime());
					elementMap.put("签退时间", signOutTime.getTime());
				}
				elementMap.put("签退地点", t.getSignOutAddress());

				if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.NORMAL.getId()) {
					if (t.getSignOutType() != null) {
						if (t.getSignOutType() == 2) {
							elementMap.put("签退状态", "异常");
							elementMap.put("签退说明", "范围外打卡");
						} else {
							elementMap.put("签退状态", "正常");
							elementMap.put("签退说明", "正常");
						}
					} else {
						elementMap.put("签退状态", "正常");
						elementMap.put("签退说明", "正常");
					}
				} else {
					elementMap.put("签退状态", "异常");
					String i = "";
					if (t.getSignOutType() != null) {
						i = t.getSignOutType() == 2 ? "-范围外打卡" : "";
					}
					if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.BE_LATE.getId()) {
						elementMap.put("签退说明", "早退" + i);
					} else if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.ABSENTEEISM.getId()) {
						elementMap.put("签退说明", "缺勤" + i);
					}
				}
				if (StrUtil.isNotEmpty(t.getNewSignOutDesc())) {
					elementMap.put("签退补卡申请", t.getNewSignOutDesc());
				}
				if(!Func.isNull(t.getUserRoleName())){
					elementMap.put("业务角色", t.getUserRoleName().split(","));
				}
				elementMap.put("当日计划保养工单数", t.getRepairPlanCount());
				if(!Func.isNull(t.getPlanType())){
					String firstSignTypeName = MaintainPlanContentEnum.getName(t.getPlanType());
					elementMap.put("当日触发首签的业务工单类型", firstSignTypeName);
				}
				if(!Func.isNull(t.getFirstSignTime())){
					elementMap.put("当日触发首签时间",t.getFirstSignTime().getTime());
				}
				if(!Func.isNull(t.getTriggerTime())){
					elementMap.put("触发首签工单关联计划日期/建单时间",t.getTriggerTime().getTime());
				}
				// 2024-07-15 新增首签工单定位异常情况
				if(!Func.isNull(t.getSignStatus())){
					String description = new Integer(1).equals(t.getSignStatus()) ? "正常"
						: new Integer(2).equals(t.getSignStatus()) ? "定位异常"
						: new Integer(3).equals(t.getSignStatus()) ? "离线签到"
						: "";
					elementMap.put("首签定位异常情况", description);
				}
				element.setFields(elementMap);
				records.add(element);
			}
			batchDTO.setRecords(records);
			// 3. 调用飞书API更新数据表、发送消息通知
			String finalResult = feiShuDocumentService.commandCompareAddBatch(appToken,tableId,batchDTO,"考勤记录ID（主键）");
			String message = "定时器或表格按钮触发更新"+FeiShuDocumentEnum.getDescription(appToken,tableId)+"结果：" + finalResult;
			feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]失败"+e);
		}
	}

	@XxlJob("galaxyElevatorDailyAttendanceCompensateJobCopy")
	public void galaxyElevatorDailyAttendanceCompensateJobCopy(){
		XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]开始");
		try{
			// 1. 数据准备：appToken，tableId
			String appToken = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_COPY.getAppToken();
			String tableId = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_COPY.getTableId();
			feiShuDocumentService.commandDistinctByKeyAndUpdateTime(appToken, tableId, "考勤记录ID（主键）","签到时间");
			Client client = feiShuConfiguration.getClient(FeishuClientEnum.TABLE);
			// 创建请求对象
			SearchAppTableRecordReqBody searchAppTableRecordReqBody = new SearchAppTableRecordReqBody();
			SearchAppTableRecordReq req = SearchAppTableRecordReq.newBuilder()
				.appToken("RTgQbRgpma1KebsyBBDcBkLenTf")
				.tableId("tblpfRvVWEjtSbi6")
				.searchAppTableRecordReqBody(searchAppTableRecordReqBody)
				.build();
			// 发起请求
			SearchAppTableRecordResp resp = client.bitable().appTableRecord().search(req);
			SearchAppTableRecordRespBody recordData = new SearchAppTableRecordRespBody();
			if(!Func.isNull(resp) && new Integer(0).equals(resp.getCode())){
				recordData = resp.getData();
			}else{
				log.warn("飞书API获取数据失败：{}", Jsons.DEFAULT.toJson(resp));
				return;
			}
			AppTableRecord[] items = recordData.getItems();
			FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
			List<FeiShuFieldsDTO> records = new ArrayList<>();
			for (AppTableRecord i : items) {
				FeiShuFieldsDTO element = new FeiShuFieldsDTO();
				Map<String, Object> elementMap = new HashMap<>(30);
				Map<String, Object> fields = i.getFields();

				Object o = fields.get("签退状态");
				// 将 o 转换成 String
				String signOutStatus = o == null ? "" : String.valueOf(o);
				if(!Func.isNull(signOutStatus)){
					if("正常".equals(signOutStatus)){
						/**
						 * 获取 fields 的以下格式：
						 * "考勤记录ID（主键）": [
						 *             {
						 *               "text": "1792208444707700739",
						 *               "type": "text"
						 *             }
						 *           ]
						 */
						// 获取上述 text 值
						String id = "";
						List<Map<String, Object>> idList = (List<Map<String, Object>>) fields.get("考勤记录ID（主键）");
						if(Func.isNotEmpty(idList)){
							Map<String, Object> idMap = idList.get(0);
							if(Func.isNotEmpty(idMap)){
								id = String.valueOf(idMap.get("text"));
							}
						}
						// 遍历 jsonArray，获取第一个元素的 text 值
						elementMap.put("考勤记录ID（主键）", id);
						elementMap.put("签退说明", "正常");
					}
				}
				if(!elementMap.isEmpty()){
					element.setFields(elementMap);
					records.add(element);
				}
			}
			batchDTO.setRecords(records);
			// 3. 调用飞书API更新数据表、发送消息通知
			String finalResult = feiShuDocumentService.commandCompareUpdateBatch(appToken,tableId,batchDTO,"考勤记录ID（主键）");
			String message = "定时器或表格按钮触发更新"+FeiShuDocumentEnum.getDescription(appToken,tableId)+"结果：" + finalResult;
			feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]失败"+e);
		}
	}

	@XxlJob("galaxyElevatorDailyAttendanceCompensateJob")
	public void galaxyElevatorDailyAttendanceCompensateJob(){
		XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]开始");
		try{
			// 1. 数据准备：appToken，tableId
			String appToken = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE.getAppToken();
			String tableId = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE.getTableId();
			feiShuDocumentService.commandDistinctByKeyAndUpdateTime(appToken, tableId, "考勤记录ID（主键）","签到时间");
			Client client = feiShuConfiguration.getClient(FeishuClientEnum.TABLE);
			// 创建请求对象
			SearchAppTableRecordReqBody searchAppTableRecordReqBody = new SearchAppTableRecordReqBody();
			SearchAppTableRecordReq req = SearchAppTableRecordReq.newBuilder()
				.appToken("RTgQbRgpma1KebsyBBDcBkLenTf")
				.tableId("tblpfRvVWEjtSbi6")
				.searchAppTableRecordReqBody(searchAppTableRecordReqBody)
				.build();
			// 发起请求
			SearchAppTableRecordResp resp = client.bitable().appTableRecord().search(req);
			SearchAppTableRecordRespBody recordData = new SearchAppTableRecordRespBody();
			if(!Func.isNull(resp) && new Integer(0).equals(resp.getCode())){
				recordData = resp.getData();
			}else{
				log.warn("飞书API获取数据失败：{}", Jsons.DEFAULT.toJson(resp));
				return;
			}
			AppTableRecord[] items = recordData.getItems();
			FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
			List<FeiShuFieldsDTO> records = new ArrayList<>();
			for (AppTableRecord i : items) {
				FeiShuFieldsDTO element = new FeiShuFieldsDTO();
				Map<String, Object> elementMap = new HashMap<>(30);
				Map<String, Object> fields = i.getFields();

				Object o = fields.get("签退状态");
				// 将 o 转换成 String
				String signOutStatus = o == null ? "" : String.valueOf(o);
				if(!Func.isNull(signOutStatus)){
					if("正常".equals(signOutStatus)){
						/**
						 * 获取 fields 的以下格式, 获取 text 值：
						 * "考勤记录ID（主键）": [
						 *             {
						 *               "text": "1792208444707700739",
						 *               "type": "text"
						 *             }
						 *           ]
						 */
						String id = "";
						List<Map<String, Object>> idList = (List<Map<String, Object>>) fields.get("考勤记录ID（主键）");
						if(Func.isNotEmpty(idList)){
							Map<String, Object> idMap = idList.get(0);
							if(Func.isNotEmpty(idMap)){
								id = String.valueOf(idMap.get("text"));
							}
						}
						// 遍历 jsonArray，获取第一个元素的 text 值
						elementMap.put("考勤记录ID（主键）", id);
						elementMap.put("签退说明", "正常");
					}
				}
				if(!elementMap.isEmpty()){
					element.setFields(elementMap);
					records.add(element);
				}
			}
			batchDTO.setRecords(records);
			// 3. 调用飞书API更新数据表、发送消息通知
			String finalResult = feiShuDocumentService.commandCompareUpdateBatch(appToken,tableId,batchDTO,"考勤记录ID（主键）");
			String message = "定时器或表格按钮触发更新"+FeiShuDocumentEnum.getDescription(appToken,tableId)+"结果：" + finalResult;
			feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表数据表-[执行]失败"+e);
		}
	}

	@Async
	public void galaxyElevatorDailyAttendancePersonCopy(UserSignDetailDTO userSignDetailDTO) {

		// 1. 数据准备：appToken，tableId，要对比新增的数据
		String appToken = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_COPY.getAppToken();
		String tableId = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_COPY.getTableId();
		// 查询当天对比新增的数据
		// 时间查询条件：今天 0:00 - 23:59
		Calendar calendar = Calendar.getInstance();
		Long userId = userSignDetailDTO.getUserId();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date startTime = calendar.getTime();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		Date endTime = calendar.getTime();
		String tenantId = "592800";
		// 根据时间及userId查询所需新增数据
		List<LarkUserSignDetailVO> targetList = userSignDetailService.getLarkSignDetailByCondition(
			tenantId, startTime, endTime, userId
		);
		if(Func.isEmpty(targetList)){
			return;
		}
		// 2. 数据处理、数据封装
		FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
		List<FeiShuFieldsDTO> records = new ArrayList<>();
		HashMap<Integer, String> weekMap = new HashMap<Integer,String>();
		weekMap.put(1, "一");
		weekMap.put(2, "二");
		weekMap.put(3, "三");
		weekMap.put(4, "四");
		weekMap.put(5, "五");
		weekMap.put(6, "六");
		weekMap.put(7, "日");
		for (LarkUserSignDetailVO t : targetList) {

			FeiShuFieldsDTO element = new FeiShuFieldsDTO();
			Map<String, Object> elementMap = new HashMap<>(30);
			if(!Func.isNull(t.getPlanType())){
				String firstSignTypeName = MaintainPlanContentEnum.getName(t.getPlanType());
				elementMap.put("当日触发首签的业务工单类型", firstSignTypeName);
			}
			elementMap.put("考勤记录ID（主键）", String.valueOf(t.getId()));
			elementMap.put("梯无忧_userID", String.valueOf(t.getTargetUserId()));
			elementMap.put("考勤日期", startTime.getTime());
			// 数字1到7分别代表星期一到星期日
			if(!Func.isNull(t.getWeek())){
				elementMap.put("星期", "星期"+weekMap.get(t.getWeek()));
			}
			elementMap.put("日期类型", t.getHoliday() == 0 ? "工作日" : "休息日");
			elementMap.put("姓名", t.getUserName());
			// 考勤类型，当前固定为维保
			elementMap.put("考勤类型", "维保");
			if(!Func.isNull(t.getSignTime())){
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// 将String 转为 Date
				Date signTime = null;
				try {
					signTime = dateFormat.parse(t.getSignTime());
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
				elementMap.put("签到时间", signTime.getTime());
			}

			elementMap.put("签到地点", t.getSignAddress());
			if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.NORMAL.getId()) {
				if (t.getSignType() != null) {
					if (t.getSignType() == 2) {
						elementMap.put("签到状态", "异常");
						elementMap.put("签到说明", "范围外打卡");
					} else {
						elementMap.put("签到状态", "正常");
					}
				} else {
					elementMap.put("签到状态", "正常");
				}
			} else {
				elementMap.put("签到状态", "异常");
				String i = "";
				if (t.getSignType() != null) {
					i = t.getSignType() == 2 ? "-范围外打卡" : "";
				}
				if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.BE_LATE.getId()) {
					elementMap.put("签到说明", "迟到" + i);
				} else if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.ABSENTEEISM.getId()) {
					elementMap.put("签到说明", "缺勤" + i);
				}
			}
			if (StrUtil.isNotEmpty(t.getNewSignDesc())) {
				elementMap.put("签到补卡申请", t.getNewSignDesc());
			}

			if(!Func.isNull(t.getSignOutTime())){
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// 将String 转为 Date
				Date signOutTime = null;
				try {
					signOutTime = dateFormat.parse(t.getSignOutTime());
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
				elementMap.put("签退时间", signOutTime.getTime());
			}
			elementMap.put("签退地点", t.getSignOutAddress());

			if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.NORMAL.getId()) {
				if (t.getSignOutType() != null) {
					if (t.getSignOutType() == 2) {
						elementMap.put("签退状态", "异常");
						elementMap.put("签退说明", "范围外打卡");
					} else {
						elementMap.put("签退状态", "正常");
						elementMap.put("签退说明", "正常");
					}
				} else {
					elementMap.put("签退状态", "正常");
					elementMap.put("签退说明", "正常");
				}
			} else {
				elementMap.put("签退状态", "异常");
				String i = "";
				if (t.getSignOutType() != null) {
					i = t.getSignOutType() == 2 ? "-范围外打卡" : "";
				}
				if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.BE_LATE.getId()) {
					elementMap.put("签退说明", "早退" + i);
				} else if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.ABSENTEEISM.getId()) {
					elementMap.put("签退说明", "缺勤" + i);
				}
			}
			if (StrUtil.isNotEmpty(t.getNewSignOutDesc())) {
				elementMap.put("签退补卡申请", t.getNewSignOutDesc());
			}
			if(!Func.isNull(t.getUserRoleName())){
				elementMap.put("业务角色", t.getUserRoleName().split(","));
			}
			elementMap.put("当日计划保养工单数", t.getRepairPlanCount());
			if(!Func.isNull(t.getPlanType())){
				String firstSignTypeName = MaintainPlanContentEnum.getName(t.getPlanType());
				elementMap.put("当日触发首签的业务工单类型", firstSignTypeName);
			}
			if(!Func.isNull(t.getTriggerTime())){
				elementMap.put("触发首签工单关联日期",t.getTriggerTime().getTime());
			}
			// 2024-07-15 新增首签工单定位异常情况
			if(!Func.isNull(t.getSignStatus())){
				String description = new Integer(1).equals(t.getSignStatus()) ? "正常"
					: new Integer(2).equals(t.getSignStatus()) ? "定位异常"
					: new Integer(3).equals(t.getSignStatus()) ? "离线签到"
					: "";
				elementMap.put("首签定位异常情况", description);
			}
			element.setFields(elementMap);
			records.add(element);
		}
		batchDTO.setRecords(records);
		// 3. 调用飞书API更新数据表、发送消息通知
		String finalResult = feiShuDocumentService.commandCompareAddBatch(appToken,tableId,batchDTO,"考勤记录ID（主键）");
		String message = "定时器或表格按钮触发更新"+FeiShuDocumentEnum.getDescription(appToken,tableId)+"结果：" + finalResult;
		feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
	}

	@Async
	public void galaxyElevatorDailyAttendancePerson(UserSignDetailDTO userSignDetailDTO) {
		// 1. 数据准备：appToken，tableId，要对比新增的数据
		String appToken = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE.getAppToken();
		String tableId = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE.getTableId();
		// 查询当天对比新增的数据
		// 时间查询条件：今天 0:00 - 23:59
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		Date startTime = calendar.getTime();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 0);
		Date endTime = calendar.getTime();
		Long userId = userSignDetailDTO.getUserId();
		String tenantId = "592800";
		// 根据时间及userId查询所需新增数据
		List<LarkUserSignDetailVO> targetList = userSignDetailService.getLarkSignDetailByCondition(
			tenantId, startTime, endTime, userId
		);
		if(Func.isEmpty(targetList)){
			return;
		}

		// 2. 数据处理、数据封装

		FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
		List<FeiShuFieldsDTO> records = new ArrayList<>();
		HashMap<Integer, String> weekMap = new HashMap<Integer,String>();
		weekMap.put(1, "一");
		weekMap.put(2, "二");
		weekMap.put(3, "三");
		weekMap.put(4, "四");
		weekMap.put(5, "五");
		weekMap.put(6, "六");
		weekMap.put(7, "日");
		for (LarkUserSignDetailVO t : targetList) {

			FeiShuFieldsDTO element = new FeiShuFieldsDTO();
			Map<String, Object> elementMap = new HashMap<>(30);
			if(!Func.isNull(t.getPlanType())){
				String firstSignTypeName = MaintainPlanContentEnum.getName(t.getPlanType());
				elementMap.put("当日触发首签的业务工单类型", firstSignTypeName);
			}
			elementMap.put("考勤记录ID（主键）", String.valueOf(t.getId()));
			elementMap.put("梯无忧_userID", String.valueOf(t.getTargetUserId()));
			elementMap.put("考勤日期", startTime.getTime());
			if(!Func.isNull(t.getGroupName())){
				elementMap.put("组名称", t.getGroupName().split(","));
			}
			// 数字1到7分别代表星期一到星期日
			if(!Func.isNull(t.getWeek())){
				elementMap.put("星期", "星期"+weekMap.get(t.getWeek()));
			}
			elementMap.put("日期类型", t.getHoliday() == 0 ? "工作日" : "休息日");
			elementMap.put("姓名", t.getUserName());
			// 考勤类型，当前固定为维保
			elementMap.put("考勤类型", "维保");
			if(!Func.isNull(t.getSignTime())){
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// 将String 转为 Date
				Date signTime = null;
				try {
					signTime = dateFormat.parse(t.getSignTime());
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
				elementMap.put("签到时间", signTime.getTime());
			}

			elementMap.put("签到地点", t.getSignAddress());
			if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.NORMAL.getId()) {
				if (t.getSignType() != null) {
					if (t.getSignType() == 2) {
						elementMap.put("签到状态", "异常");
						elementMap.put("签到说明", "范围外打卡");
					} else {
						elementMap.put("签到状态", "正常");
					}
				} else {
					elementMap.put("签到状态", "正常");
				}
			} else {
				elementMap.put("签到状态", "异常");
				String i = "";
				if (t.getSignType() != null) {
					i = t.getSignType() == 2 ? "-范围外打卡" : "";
				}
				if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.BE_LATE.getId()) {
					elementMap.put("签到说明", "迟到" + i);
				} else if (t.getSignBeLate() != null && t.getSignBeLate() == SignStatusEnum.ABSENTEEISM.getId()) {
					elementMap.put("签到说明", "缺勤" + i);
				}
			}
			if (StrUtil.isNotEmpty(t.getNewSignDesc())) {
				elementMap.put("签到补卡申请", t.getNewSignDesc());
			}

			if(!Func.isNull(t.getSignOutTime())){
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// 将String 转为 Date
				Date signOutTime = null;
				try {
					signOutTime = dateFormat.parse(t.getSignOutTime());
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
				elementMap.put("签退时间", signOutTime.getTime());
			}
			elementMap.put("签退地点", t.getSignOutAddress());

			if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.NORMAL.getId()) {
				if (t.getSignOutType() != null) {
					if (t.getSignOutType() == 2) {
						elementMap.put("签退状态", "异常");
						elementMap.put("签退说明", "范围外打卡");
					} else {
						elementMap.put("签退状态", "正常");
						elementMap.put("签退说明", "正常");
					}
				} else {
					elementMap.put("签退状态", "正常");
					elementMap.put("签退说明", "正常");
				}
			} else {
				elementMap.put("签退状态", "异常");
				String i = "";
				if (t.getSignOutType() != null) {
					i = t.getSignOutType() == 2 ? "-范围外打卡" : "";
				}
				if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.BE_LATE.getId()) {
					elementMap.put("签退说明", "早退" + i);
				} else if (t.getSignOutLeaveEarly() != null && t.getSignOutLeaveEarly() == SignStatusEnum.ABSENTEEISM.getId()) {
					elementMap.put("签退说明", "缺勤" + i);
				}
			}
			if (StrUtil.isNotEmpty(t.getNewSignOutDesc())) {
				elementMap.put("签退补卡申请", t.getNewSignOutDesc());
			}
			if(!Func.isNull(t.getUserRoleName())){
				elementMap.put("业务角色", t.getUserRoleName().split(","));
			}
			elementMap.put("当日计划保养工单数", t.getRepairPlanCount());
			if(!Func.isNull(t.getPlanType())){
				String firstSignTypeName = MaintainPlanContentEnum.getName(t.getPlanType());
				elementMap.put("当日触发首签的业务工单类型", firstSignTypeName);
			}
			if(!Func.isNull(t.getFirstSignTime())){
				elementMap.put("当日触发首签时间",t.getFirstSignTime().getTime());
			}
			if(!Func.isNull(t.getTriggerTime())){
				elementMap.put("触发首签工单关联计划日期/建单时间",t.getTriggerTime().getTime());
			}
			// 2024-07-15 新增首签工单定位异常情况
			if(!Func.isNull(t.getSignStatus())){
				String description = new Integer(1).equals(t.getSignStatus()) ? "正常"
					: new Integer(2).equals(t.getSignStatus()) ? "定位异常"
					: new Integer(3).equals(t.getSignStatus()) ? "离线签到"
					: "";
				elementMap.put("首签定位异常情况", description);
			}
			element.setFields(elementMap);
			records.add(element);
		}
		batchDTO.setRecords(records);
		// 3. 调用飞书API更新数据表、发送消息通知
		String finalResult = feiShuDocumentService.commandCompareAddBatch(appToken,tableId,batchDTO,"考勤记录ID（主键）");
		String message = "定时器或表格按钮触发更新"+FeiShuDocumentEnum.getDescription(appToken,tableId)+"结果：" + finalResult;
		feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
	}

	/**
	 * 每天9：10 执行 凤仪的考勤统计表
	 */
	@XxlJob("galaxyElevatorDailyAttendanceStaticCopyJob")
	public void galaxyElevatorDailyAttendanceStaticCopyJob(){
		XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表统计数据表-[执行]开始");
		try{
			// 1. 数据准备：appToken，tableId，要对比新增的数据
			String appToken = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_STATISTICS_COPY.getAppToken();
			String tableId = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_STATISTICS_COPY.getTableId();
			feiShuDocumentService.commandDistinctByKeyAndUpdateTime(appToken,tableId,"考勤记录ID（主键）","考勤日期");
			List<LarkElevatorDailyAttendanceStaticVO> targetList = userSignDetailService.getLarkElevatorDailyAttendanceStatic();
			if(Func.isEmpty(targetList)){
				return;
			}
			List<LarkElevatorDailyAttendanceStaticVO> fullList = userSignDetailService.getLarkElevatorDailyAttendanceStaticFull();
			if(!Func.isEmpty(fullList)){
				targetList.forEach(t->{
					// 获取 fullList 中对应 userId 的数据，单个对象
					LarkElevatorDailyAttendanceStaticVO l = fullList.stream().filter(f -> f.getTargetUserId().equals(t.getTargetUserId())).findFirst().orElseGet(null);
					if(!Func.isNull(l)){
						t.setUnfinishedTotalCountsFull(l.getUnfinishedTotalCountsFull());
						t.setUnfinishedAnnualReviewCountsFull(l.getUnfinishedAnnualReviewCountsFull());
						t.setUnfinishedRepairCountsFull(l.getUnfinishedRepairCountsFull());
						t.setUnfinishedNotAnnualReviewCountsFull(l.getUnfinishedNotAnnualReviewCountsFull());
					}
				});
			}
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date startTime = calendar.getTime();
			// 2. 数据处理、数据封装
			FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
			List<FeiShuFieldsDTO> records = new ArrayList<>();
			for (LarkElevatorDailyAttendanceStaticVO t : targetList) {
				FeiShuFieldsDTO element = new FeiShuFieldsDTO();
				Map<String, Object> elementMap = new HashMap<>(30);
				elementMap.put("考勤记录ID（主键）", String.valueOf(t.getSignId()));
				elementMap.put("梯无忧_userID", String.valueOf(t.getTargetUserId()));
				elementMap.put("考勤日期", startTime.getTime());
				elementMap.put("姓名", t.getUserName());
				// 考勤类型，当前固定为维保
				elementMap.put("当日计划保养工单数", t.getPlanMaintainCounts());
				elementMap.put("考勤当日，9:00前未完成工单总数（复合工单类型）", t.getUnfinishedTotalCounts());
				elementMap.put("考勤日9点时未完成保养工单总数", t.getUnfinishedPlanMaintainCounts());
				elementMap.put("考勤日，9点时未完成保养（仅年审梯）",t.getUnfinishedAnnualReviewCounts());
				elementMap.put("考勤日，9点时未完成保养（仅非年审梯）",t.getUnfinishedNotAnnualReviewCounts());
				elementMap.put("考勤日，6~9点时是否有救援单",t.getRescueHappenedEarlyCounts());
				if(!Func.isNull(t.getLastSignOutTime())){
					elementMap.put("考勤日前一天，最后一次工单签退时间（用于判断是否跨天签退）",t.getLastSignOutTime().getTime());
				}
				elementMap.put("【全量】截止至考勤日9点，未完成保养工单总数",t.getUnfinishedRepairCountsFull());
				elementMap.put("【全量】截止至考勤日9点，未完成保养（仅年审梯）",t.getUnfinishedAnnualReviewCountsFull());
				elementMap.put("【全量】截止至考勤日9点，未完成保养（仅非年审梯）",t.getUnfinishedNotAnnualReviewCountsFull());
				elementMap.put("【全量】截止至考勤日9点，未完成工单总数（复合工单类型）",t.getUnfinishedTotalCountsFull());
				elementMap.put("当日计划保养数（年审梯）",t.getPlanAnnualReviewCounts());
				elementMap.put("当日计划保养数（非年审梯）",t.getPlanNotAnnualReviewCounts());
				// 2024-07-17 新增 当日计划保养数—夜间跨天（年审梯）
				elementMap.put("当日计划保养数—夜间跨天（年审梯）",t.getMaintainNightCounts());
				element.setFields(elementMap);
				records.add(element);
			}
			batchDTO.setRecords(records);
			// 3. 调用飞书API更新数据表、发送消息通知
			String finalResult = feiShuDocumentService.commandCompareAddBatch(appToken,tableId,batchDTO,"考勤记录ID（主键）");
			String message = "定时器触发更新"+FeiShuDocumentEnum.getDescription(appToken,tableId)+"结果：" + finalResult;
			feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤统计数据表数据表-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤统计数据表数据表-[执行]失败"+e);
		}
	}

	/**
	 * 每天9：10 执行 凤仪的考勤统计表
	 */
	@XxlJob("galaxyElevatorDailyAttendanceStaticJob")
	public void galaxyElevatorDailyAttendanceStaticJob(){
		XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表统计数据表-[执行]开始");
		try{
			// 1. 数据准备：appToken，tableId，要对比新增的数据
			String appToken = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_STATISTICS.getAppToken();
			String tableId = FeiShuDocumentEnum.ELEVATOR_DAILY_ATTENDANCE_STATISTICS.getTableId();
			// 根据主键去重
			feiShuDocumentService.commandDistinctByKeyAndUpdateTime(appToken,tableId,"考勤记录ID（主键）","考勤日期");
			List<LarkElevatorDailyAttendanceStaticVO> targetList = userSignDetailService.getLarkElevatorDailyAttendanceStatic();
			if(Func.isEmpty(targetList)){
				return;
			}
			List<LarkElevatorDailyAttendanceStaticVO> fullList = userSignDetailService.getLarkElevatorDailyAttendanceStaticFull();
			if(!Func.isEmpty(fullList)){
				targetList.forEach(t->{
					// 获取 fullList 中对应 userId 的数据，单个对象
					LarkElevatorDailyAttendanceStaticVO l = fullList.stream().filter(f -> f.getTargetUserId().equals(t.getTargetUserId())).findFirst().orElseGet(null);
					if(!Func.isNull(l)){
						t.setUnfinishedTotalCountsFull(l.getUnfinishedTotalCountsFull());
						t.setUnfinishedAnnualReviewCountsFull(l.getUnfinishedAnnualReviewCountsFull());
						t.setUnfinishedRepairCountsFull(l.getUnfinishedRepairCountsFull());
						t.setUnfinishedNotAnnualReviewCountsFull(l.getUnfinishedNotAnnualReviewCountsFull());
					}
				});
			}
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date startTime = calendar.getTime();
			// 2. 数据处理、数据封装
			FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
			List<FeiShuFieldsDTO> records = new ArrayList<>();
			for (LarkElevatorDailyAttendanceStaticVO t : targetList) {
				FeiShuFieldsDTO element = new FeiShuFieldsDTO();
				Map<String, Object> elementMap = new HashMap<>(30);
				elementMap.put("考勤记录ID（主键）", String.valueOf(t.getSignId()));
				elementMap.put("梯无忧_userID", String.valueOf(t.getTargetUserId()));
				elementMap.put("考勤日期", startTime.getTime());
				elementMap.put("姓名", t.getUserName());
				// 考勤类型，当前固定为维保
				elementMap.put("当日计划保养工单数", t.getPlanMaintainCounts());
				elementMap.put("考勤当日，9:00前未完成工单总数（复合工单类型）", t.getUnfinishedTotalCounts());
				elementMap.put("考勤日9点时未完成保养工单总数", t.getUnfinishedPlanMaintainCounts());
				elementMap.put("考勤日，9点时未完成保养（仅年审梯）",t.getUnfinishedAnnualReviewCounts());
				elementMap.put("考勤日，9点时未完成保养（仅非年审梯）",t.getUnfinishedNotAnnualReviewCounts());
				elementMap.put("考勤日，6~9点时是否有救援单",t.getRescueHappenedEarlyCounts());
				if(!Func.isNull(t.getLastSignOutTime())){
					elementMap.put("考勤日前一天，最后一次工单签退时间（用于判断是否跨天签退）",t.getLastSignOutTime().getTime());
				}
				elementMap.put("【全量】截止至考勤日9点，未完成保养工单总数",t.getUnfinishedRepairCountsFull());
				elementMap.put("【全量】截止至考勤日9点，未完成保养（仅年审梯）",t.getUnfinishedAnnualReviewCountsFull());
				elementMap.put("【全量】截止至考勤日9点，未完成保养（仅非年审梯）",t.getUnfinishedNotAnnualReviewCountsFull());
				elementMap.put("【全量】截止至考勤日9点，未完成工单总数（复合工单类型）",t.getUnfinishedTotalCountsFull());
				elementMap.put("当日计划保养数（年审梯）",t.getPlanAnnualReviewCounts());
				elementMap.put("当日计划保养数（非年审梯）",t.getPlanNotAnnualReviewCounts());
				// 2024-07-17 新增 当日计划保养数—夜间跨天（年审梯）
				elementMap.put("当日计划保养数—夜间跨天（年审梯）",t.getMaintainNightCounts());
				element.setFields(elementMap);
				records.add(element);
			}
			batchDTO.setRecords(records);
			// 3. 调用飞书API更新数据表、发送消息通知
			String finalResult = feiShuDocumentService.commandCompareAddBatch(appToken,tableId,batchDTO,"考勤记录ID（主键）");
			String message = "定时器触发更新"+FeiShuDocumentEnum.getDescription(appToken,tableId)+"结果：" + finalResult;
			feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤统计数据表数据表-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤统计数据表数据表-[执行]失败"+e);
		}
	}

	@XxlJob("galaxyElevatorDailyAttendanceStaticCompensateJobCopy")
	public void galaxyElevatorDailyAttendanceStaticCompensateJobCopy() {
		XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表统计数据表-[执行]开始");
		try{
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤统计数据表数据表-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤统计数据表数据表-[执行]失败"+e);
		}
	}

	@XxlJob("galaxyElevatorDailyAttendanceStaticCompensateJob")
	public void galaxyElevatorDailyAttendanceStaticCompensateJob() {
		XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤数据表统计数据表-[执行]开始");
		try{
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤统计数据表数据表-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新凤仪的飞书考勤统计数据表数据表-[执行]失败"+e);
		}
	}
}
